<?php
/**
 *
 * 版权所有：顺势网站管理系统<qwadmin.qiawei.com>

 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：顾问列表
 *
 **/

namespace Qwadmin\Controller;
use Qwadmin\Model\PhotosModel;
use Vendor\Tree;
class PageController extends ComController
{
    public function index() {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';      
        $prefix = C('DB_PREFIX');
        $cid = isset($_GET['cid']) ? $_GET['cid'] : ''; 
        $order = 'id desc';
        $pages = M('pages');
        $orderby = "id desc";
        $pagesize = 20;//每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $pages->where($where)->count();
        $list = $pages->field("{$prefix}pages.*,{$prefix}page_category.name")->where($where)->order($orderby)->join("{$prefix}page_category ON {$prefix}page_category.id = {$prefix}pages.cid")->limit($offset . ',' . $pagesize)->select();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('pages', $list);
        // $this->assign('info',$article);
        $this->assign('page', $page);
        $this->display();
    }

    public function add() {
        //获取单页分类
        $category = M('page_category')->field('id,name')->order('time asc')->select();
        $tree = new Tree($category);
        $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
        $category = $tree->get_tree(0, $str, $sid);
        $this->assign('category', $category);//导航
        $content = M('pages')->select();
        $this->assign('pages',$pages);
        $this->display('form');
    }
    public function edit() {
        $id = isset($_GET['id']) ? intval($_GET['id']) : false;
        $pages=M("pages")->where(['id'=>$id])->find();
        //获取单页分类
        $category = M('page_category')->field('id,name')->order('time asc')->select();
        $tree = new Tree($category);
        $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
        $category = $tree->get_tree(0, $str, $pages['cid']);
        $this->assign('category', $category);//导航
        
        if (empty($id)) {
           $this->error('参数错误');
        }
        
        $this->assign('pages',$pages);
        
        $this->display('form');
    }

    public function update() {
            $data['id'] = I('post.id', false, 'intval');
            $data['cid'] = I('post.cid', false, 'intval');
            $data['title']    =        I('post.title', '', 'htmlspecialchars');
            $data['content'] = html_entity_decode(isset($_POST['content']) ? $_POST['content'] : false);
            $data['keywords']    =      I('post.keywords', '', 'htmlspecialchars');
            $data['seotitle']     =     I('post.seotitle', '', 'htmlspecialchars');
            $data['description']  =        I('post.description', '', 'htmlspecialchars');
            $data['thumbnail'] = I('post.thumbnail', '', 'strip_tags');
            $data['o']       =    I('post.o', '', 'htmlspecialchars');
            $data['dir']    =    I('post.dir', '', 'htmlspecialchars');
            $data['t']     = date('Y-m-d H:i:s');
        //var_dump($data);exit;
        $pages = M('pages');
        if (empty($data['id'])) { //增加
            if ($pages->add($data)) {
               $this->success('添加单页成功');
            } else {
                $this->error('添加单页失败');
            }
        } else { //编辑
            if ($pages->save($data)) {
                $this->success('编辑单页成功');
            } else {
                $this->error('编辑单页失败');
            }
        }

    }

    public function del() {

        $ids = isset($_REQUEST['id']) ? $_REQUEST['id'] : false;
        if(is_array($uids)){
            $map['id'] = array('in', $ids);
        }else{
            $map['id'] = $ids;
        }
        if (M('pages')->where($map)->delete()) {
            addlog('删除单页ID：' . $ids);
            $this->success('单页删除成功！');
        } else {
            $this->error('参数错误！');
        }
    }
    //单页分类
    public function cate_index(){
        $info=M("page_category")->order('id desc')->select();
        $this->assign("info",$info);
        $this->display();
    }
    //新增分类
    public function cate_add(){
        $this->display();
    }
    //修改分类
    public function cate_edit(){
        $id=$_GET['id'];
        // var_dump($id);exit;
        $info=M("page_category")->find($id);
        $this->assign("info",$info);
        $this->display("cate_add");
    }
    //保存分类
    public function cate_update(){
            $data['id'] = I('post.id', false, 'intval');
            $data['name']    = I('post.name', '', 'htmlspecialchars');
            $data['time']     = date('Y-m-d H:i:s');
        //var_dump($data);exit;
        $pages = M('page_category');
        if (empty($data['id'])) { //增加
            if ($pages->add($data)) {
               $this->success('添加单页分类成功');
            } else {
                $this->error('添加单页分类失败');
            }
        } else { //编辑
            if ($pages->save($data)) {
                $this->success('编辑单页分类成功');
            } else {
                $this->error('编辑单页分类失败');
            }
        }

    }
    //删除分类
    public function cate_del(){

    }

}